package org.example.Service.Impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.example.Dao.EmpDao;
import org.example.JavaBean.Emp;
import org.example.Service.EmpService;
import org.example.util.DateUtil;
import org.example.util.JsonResult;
import org.example.util.ResultTool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @Auther:Liu
 * @Testname:EmpServiceImpl
 * @Date:2025/7/29 9:58
 */
@Service("EmpServiceImpl")
public class EmpServiceImpl implements EmpService {
    @Autowired
    private EmpDao empDao;
    @Override
    public JsonResult findAll() {
        List<Emp> all = empDao.findAll();
       if (all == null||all.size()==0) {
           return ResultTool.fail(500,"查询数据失败");
       }else{
           return ResultTool.success(all);
       }
    }

    @Override
    public JsonResult findByPage(int page, int size) {
        PageHelper.startPage(page, size);
        List<Emp> list = empDao.findAll();
        PageInfo<Emp> pageInfo = new PageInfo<Emp>(list);
        return ResultTool.success(pageInfo);
    }

    @Override
    public JsonResult findByName(String ename) {
        if (ename==null||ename.equals("")) {
            return ResultTool.fail(500,"无该信息");
        }else{
            List<Emp> byName = empDao.findByName("%" + ename + "%");
            return ResultTool.success(byName);
        }
    }

    @Override
    public JsonResult Insert(Emp emp) {
        int i = empDao.Insert(emp);
        if (emp==null||i>0) {
            return ResultTool.fail(500,"添加失败");
        }else{
//            empDao.Insert(new Emp(null,"张三","工人",2000,s,45.45,200.00,30,1));
            return ResultTool.success("success");
        }
    }

    @Override
    public JsonResult Delete(Integer id) {
        int i = empDao.delete(id);
        if (i>0) {
            return ResultTool.success("删除成功");
        }else{
            return ResultTool.fail(500,"删除失败");
        }
    }

    @Override
    public JsonResult Update(Emp emp) {
        int i = empDao.update(emp);
        if (i>0) {
            return ResultTool.success("更新成功");
        }else{
            return ResultTool.fail(500,"更新失败");
        }
    }

    @Override
    public JsonResult UpdateOrInsert(Emp emp) {
        Integer i = emp.getEmpno();
        if (i==null||i==0) {
            int i1 = empDao.Insert(emp);
            return ResultTool.success("添加成功");
        }else{
            int update = empDao.update(emp);
            return ResultTool.success("更新成功");
        }
    }
}
